package com.geping.etl.common.service.impl;

import com.geping.etl.common.dto.AddReportDto;
import com.geping.etl.common.entity.Report_Info;
import com.geping.etl.common.repository.Report_InfoRepository;
import com.geping.etl.common.service.Report_InfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

@Service
@Transactional
public class Report_InfoServiceImpl implements Report_InfoService {
    @Autowired
    private Report_InfoRepository reportinfopository;

    //显示所有报表权限
    @Override
    public List<Report_Info> findAll() {

        return reportinfopository.findAll();
    }

    @Override
    public List<Report_Info> findAllBySome(Specification<Report_Info> spec) {

        return reportinfopository.findAll(spec);
    }

    @Override
    public void updateReportInfoPending(Report_Info ri) {

        reportinfopository.updateReportInfoPending(ri);
    }

    @Override
    public void updateReportInfoPendingAudit(Report_Info ri) {

        reportinfopository.updateReportInfoPendingAudit(ri);
    }

    @Override
    public void updateReportInfoCheckFail(Report_Info ri) {

        reportinfopository.updateReportInfoCheckFail(ri);
    }

    @Override
    public void updateReportInfoCheckSuccess(Report_Info ri) {

        reportinfopository.updateReportInfoCheckSuccess(ri);
    }

    @Override
    public Page<Report_Info> findAll(Specification<Report_Info> spec, Pageable pageable) {

        return reportinfopository.findAll(spec, pageable);
    }

    @Override
    public List<Report_Info> getReportBySubjectId(String subjectId, String east2HuachenFlag) {
            return reportinfopository.getReportBySubjectId(subjectId);
    }

    @Override
    @Transactional
    public Report_Info addReportInfo(AddReportDto addReportDto) {
        Integer maxId = reportinfopository.findMaxId();
        Report_Info report_Info = new Report_Info();
        if (Objects.isNull(maxId)) {
            maxId = 0;
        }
        report_Info.setId(maxId + 1);
        report_Info.setSubjectId("2");
        String tableENUp = addReportDto.getTableEN().toUpperCase();
        report_Info.setCode(tableENUp);
        report_Info.setName(addReportDto.getTableCom());
        report_Info.setInstruction(addReportDto.getRemarks());
        return reportinfopository.save(report_Info);
    }

    @Override
    @Transactional
    public int delReportInfo(AddReportDto addReportDto) {
        return reportinfopository.deleteReport(addReportDto.getTableEN());
    }

    @Override
    public void editReportInfo(AddReportDto addReportDto) {
        reportinfopository.updateReportInfoInstruction(addReportDto.getTableEN(), addReportDto.getTableCom(),
                addReportDto.getRemarks(), addReportDto.getUploadFrequency(), addReportDto.getReportStatus());
    }


}
